package com.example.demo.metro.api;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.R;
import com.example.demo.group.ValidGroup;
import com.example.demo.metro.dtovo.FeedBackDTO;
import com.example.demo.metro.entity.FeedBack;
import com.example.demo.metro.service.FeedBackService;
import com.example.demo.metro.service.impl.FeedBackServiceImpl;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author : yuanchuan
 * @date : 2023-11-22 20:37
 * @team : 西南综合开发支持部
 * @copyright :
 **/
@RestController
@RequestMapping(value = "/api/common")
public class FeedBackApi {
    @Resource
    @Qualifier(value = FeedBackServiceImpl.BEAN_NAME)
    private FeedBackService noticeService;

    /**
     *  获取详情
     * @param id
     * @return : com.alibaba.fastjson.JSONObject
     * @author : Administrator
     * @date : 2023/11/22 022 20:41
    **/
    @GetMapping(value = "/feedback/{id}")
    public R getNotice(@PathVariable String id){
       return R.ok( noticeService.getNoticeById(id));
    }

    /**
     *  获取列表
     * @param
     * @return : com.example.demo.R
     * @author : Administrator
     * @date : 2023/11/22 022 21:28
    **/
    @GetMapping(value = "/feedback/list")
    public JSONObject list(String title, int pageNum, int pageSize){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 200);
        jsonObject.put("msg", "查询成功");
        Page<FeedBack> page = noticeService.findDataList(title, pageNum, pageSize);
        jsonObject.put("total", page.getTotalElements());
        jsonObject.put("rows", page.getContent());
        return jsonObject;
    }

    /**
     *  添加
     * @param data
     * @return : com.alibaba.fastjson.JSONObject
     * @author : Administrator
     * @date : 2023/11/22 022 20:41
    **/
    @PostMapping(value = "/feedback")
    public R save(@RequestBody @Validated(ValidGroup.add.class) FeedBackDTO data){
        noticeService.save(data);
       return R.ok();
    }

    /**
     *  修改
     * @param data
     * @return : com.alibaba.fastjson.JSONObject
     * @author : Administrator
     * @date : 2023/11/22 022 20:41
    **/
    @PutMapping(value = "/feedback")
    public R update(@RequestBody  @Validated(ValidGroup.update.class) FeedBackDTO data){
        noticeService.update(data);
       return R.ok();
    }

    /**
     *  删除
     * @param id
     * @return : com.alibaba.fastjson.JSONObject
     * @author : Administrator
     * @date : 2023/11/22 022 20:41
     **/
    @DeleteMapping(value = "/feedback/{id}")
    public R delete(@PathVariable String id){
        noticeService.deleteByIds(id);
        return R.ok();
    }
}
